import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../layout/index.vue'

const routes = [
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta: { title: '注册' }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/dashboard/index.vue'),
        meta: { title: '首页' }
      },
      {
        path: 'digital-human',
        name: 'DigitalHuman',
        redirect: '/digital-human/generate',
        meta: { title: '数字人', icon: 'User' },
        children: [
          {
            path: 'generate',
            name: 'DigitalHumanGenerate',
            component: () => import('../views/digitalHuman/generate.vue'),
            meta: { title: '数字人生成' }
          },
          {
            path: 'list',
            name: 'DigitalHumanList',
            component: () => import('../views/digitalHuman/list.vue'),
            meta: { title: '数字人生成列表' }
          }
        ]
      },
      {
        path: 'marketing-video',
        name: 'MarketingVideo',
        redirect: '/marketing-video/generate',
        meta: { title: '营销数字人', icon: 'VideoCamera' },
        children: [
          {
            path: 'generate',
            name: 'MarketingVideoGenerate',
            component: () => import('../views/marketingVideo/generate.vue'),
            meta: { title: '营销数字人生成' }
          },
          {
            path: 'list',
            name: 'MarketingVideoList',
            component: () => import('../views/marketingVideo/list.vue'),
            meta: { title: '营销数字人列表' }
          }
        ]
      },
      {
        path: 'video-mix',
        name: 'VideoMix',
        redirect: '/video-mix/generate',
        meta: { title: '视频混剪', icon: 'VideoCamera' },
        children: [
          {
            path: 'generate',
            name: 'VideoMixGenerate',
            component: () => import('@/views/videoMix/generate.vue'),
            meta: { title: '生成混剪视频' }
          },
          {
            path: 'list',
            name: 'VideoMixList',
            component: () => import('@/views/videoMix/list.vue'),
            meta: { title: '生成列表' }
          }
        ]
      },
      {
        path: 'voice',
        name: 'Voice',
        redirect: '/voice/clone',
        meta: { title: '声音克隆', icon: 'Microphone' },
        children: [
          {
            path: 'clone',
            name: 'VoiceClone',
            component: () => import('../views/voice/clone.vue'),
            meta: { title: '新建音色' }
          },
          {
            path: 'tts',
            name: 'VoiceTTS',
            component: () => import('../views/voice/tts.vue'),
            meta: { title: '文字转语音' }
          }
        ]
      },
      {
        path: 'image-generate',
        name: 'ImageGenerate',
        component: () => import('../views/imageGenerate/index.vue'),
        meta: { title: '文生图', icon: 'Picture' }
      },
      {
        path: 'transfer-all',
        name: 'TransferAll',
        redirect: '/transfer-all/video-generate',
        meta: { title: '万物迁移', icon: 'Document' },
        children: [
          {
            path: 'video-generate',
            name: 'TransferAllVideoGenerate',
            component: () => import('../views/maskEditor/index.vue'),
            meta: { title: '万物迁移视频生成' }
          },
          {
            path: 'background',
            name: 'TransferAllBackground',
            component: () => import('../views/maskEditor/background.vue'),
            meta: { title: '万物迁移-换产品背景' }
          },
          {
            path: 'task-list',
            name: 'TransferAllTaskList',
            component: () => import('../views/maskEditor/taskList.vue'),
            meta: { title: '万物迁移列表' }
          }
        ]
      },
      {
        path: 'recharge',
        name: 'Recharge',
        redirect: '/recharge/index',
        meta: { title: '充值中心', icon: 'Wallet' },
        children: [
          {
            path: 'index',
            name: 'RechargeIndex',
            component: () => import('../views/recharge/index.vue'),
            meta: { title: '充值' }
          },
          {
            path: 'records',
            name: 'RechargeRecords',
            component: () => import('../views/recharge/records.vue'),
            meta: { title: '消费记录' }
          }
        ]
      },
      {
        path: 'mask-editor',
        name: 'MaskEditor',
        redirect: '/mask-editor/index',
        meta: { title: '遮罩编辑器', icon: 'Edit' },
        children: [
          {
            path: 'index',
            name: 'MaskEditorIndex',
            component: () => import('../views/maskEditor/index.vue'),
            meta: { title: '遮罩编辑器' }
          },
          {
            path: 'task-list',
            name: 'MaskEditorTaskList',
            component: () => import('../views/maskEditor/taskList.vue'),
            meta: { title: '万物迁移列表' }
          }
        ]
      },
      {
        path: 'imageToVideo',
        name: 'ImageToVideo',
        component: () => import('../views/maskEditor/imageToVideo.vue'),
        meta: { title: '图转视频' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 添加全局前置守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token') // 假设使用localStorage存储token
  
  // 不需要登录的页面：登录页、注册页、首页（dashboard）
  const publicPages = ['Login', 'Register', 'Dashboard']
  
  // 如果访问的是公开页面，直接放行
  if (publicPages.includes(to.name)) {
    next()
    return
  }
  
  // 如果用户未登录且访问需要登录的页面，则重定向到登录页
  if (!isAuthenticated) {
    next({ name: 'Login' })
  } else {
    // 已登录，允许访问
    next()
  }
})

export default router
